package cn.anecansaitin.firecrafting.common.crafting;

import cn.anecansaitin.firecrafting.ModConstant;
import cn.anecansaitin.firecrafting.api.common.crafting.IFireRecipe;
import net.minecraft.core.Registry;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

//统一注册配方类型和配方序列化器
public class ModRecipeTypes {
    //获取配方类型注册器
    public static final DeferredRegister<RecipeType<?>> RECIPE = DeferredRegister.create(Registry.RECIPE_TYPE_REGISTRY, ModConstant.MOD_ID);
    //获取序列化器注册器
    public static final DeferredRegister<RecipeSerializer<?>> SERIALIZER = DeferredRegister.create(ForgeRegistries.RECIPE_SERIALIZERS, ModConstant.MOD_ID);
    //注册配方类型
    public static final RegistryObject<RecipeType<IFireRecipe>> FIRE_TYPE = RECIPE.register(IFireRecipe.TYPE_ID, ModRecipeType::new);
    //注册序列化器
    public static final RegistryObject<FireItemRecipe.Serializer> FIRE_ITEM_SERIALIZER = SERIALIZER.register(FireItemRecipe.Serializer.NAME.substring(ModConstant.MOD_ID.length() + 1), FireItemRecipe.Serializer::new);
    public static final RegistryObject<FireBlockRecipe.Serializer> FIRE_BLOCK_SERIALIZER = SERIALIZER.register(FireBlockRecipe.Serializer.NAME.substring(ModConstant.MOD_ID.length() + 1), FireBlockRecipe.Serializer::new);
    public static final RegistryObject<FireEntityRecipe.Serializer> FIRE_ENTITY_SERIALIZER = SERIALIZER.register(FireEntityRecipe.Serializer.NAME.substring(ModConstant.MOD_ID.length() + 1), FireEntityRecipe.Serializer::new);
    public static final RegistryObject<FireFluidRecipe.Serializer> FIRE_FLUID_SERIALIZER = SERIALIZER.register(FireFluidRecipe.Serializer.NAME.substring(ModConstant.MOD_ID.length() + 1), FireFluidRecipe.Serializer::new);
    public static final RegistryObject<FireEnergyRecipe.Serializer> FIRE_ENERGY_SERIALIZER = SERIALIZER.register(FireEnergyRecipe.Serializer.NAME.substring(ModConstant.MOD_ID.length() + 1), FireEnergyRecipe.Serializer::new);
    public static final RegistryObject<FireEffectRecipe.Serializer> FIRE_EFFECT_SERIALIZER = SERIALIZER.register(FireEffectRecipe.Serializer.NAME.substring(ModConstant.MOD_ID.length() + 1), FireEffectRecipe.Serializer::new);
    public static RegistryObject<FireBotaniaManaRecipe.Serializer> FIRE_BOTANIA_MANA_SERIALIZER;

    static {
        ModList modList = ModList.get();
        if (modList.isLoaded("botania")) {
            FIRE_BOTANIA_MANA_SERIALIZER = SERIALIZER.register(FireBotaniaManaRecipe.Serializer.NAME.substring(ModConstant.MOD_ID.length() + 1), FireBotaniaManaRecipe.Serializer::new);
        }
    }

    //注册器的调用
    public static void registry(IEventBus bus) {
        SERIALIZER.register(bus);
        RECIPE.register(bus);
    }

    private static class ModRecipeType<T extends Recipe<?>> implements RecipeType<T> {
        @Override
        public String toString() {
            return Registry.RECIPE_TYPE.getKey(this).toString();
        }
    }
}
